package com.dao;

import java.sql.*;

public class PurchaseJdbc {
    Connection conn;
    Statement stem;
    ResultSet rs ;
    public  void  lianjie (){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://120.76.45.216:3306/buy_ticket","root","root");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ResultSet show(){
        this.lianjie();
        // 向数据库发送sql 语句的
        try {
            stem = conn.createStatement();
            rs = stem.executeQuery(" select  * from  purchase");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  rs;
    }
    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public ResultSet selectById(int id){
        this.lianjie();
        // 向数据库发送sql 语句的
        try {
            stem = conn.createStatement();
            rs = stem.executeQuery(" select  * from  purchase where id = '"+id+"'");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  rs;
    }
    public  void  close(){
        try {
            if(conn!=null){
                conn.close();
            }

            if(stem!=null){
                stem.close();
            }

            if(rs!=null){
                rs.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void delete(String id) {
        this.lianjie();
        try {
            stem=conn.createStatement();
            stem.executeUpdate("delete from purchase where id = '"+id+"'");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.close();
    }
}
